package com.warmheart.controller.platform;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.warmheart.base.annotation.Auth;
import com.warmheart.base.annotation.Duplicate;
import com.warmheart.base.controller.BaseController;
import com.warmheart.base.page.PageInfo;
import com.warmheart.core.enums.ResultCode;
import com.warmheart.core.result.CommonResult;
import com.warmheart.core.util.OperatorUtil;
import com.warmheart.core.util.ParamMapUtil;
import com.warmheart.entity.platform.PlatformBase;
import com.warmheart.service.platform.IPlatformBaseService;

/**
 * 
 * @ClassName: PlatformBaseController
 * @Description: 平台控制器
 * @author ZhangJianyang
 * @date 2025年06月16日 下午13:05:35
 *
 */
@RestController
@RequestMapping("/platform/platformBase")
public class PlatformBaseController extends BaseController {
    
    private static final Logger logger = LoggerFactory.getLogger(PlatformBaseController.class);

    // 注入平台service
    @Autowired(required = false)
    private IPlatformBaseService platformBaseService;

    /**
     * 
     * @Title: listData
     * @Description: 查询平台列表数据
     * @author ZhangJianyang
     * @date 2025年06月16日 下午13:05:35
     * @param request
     * @param response
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @RequestMapping("/listData")
    public CommonResult<Map<String, Object>> listData(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 参数数据
        Map<String, Object> jsonMap = getJsonMap(request);
        
        // 返回结果
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        // 参数对象
        PlatformBase platformBase = new PlatformBase();
        
        // 每页记录数
        Integer pageSize = ParamMapUtil.getInteger(jsonMap, "pageSize");
        if (pageSize == null) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "每页记录数不能为空");
        }
        if (pageSize <= 0) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "每页记录数必须大于0");
        }
        platformBase.setPageSize(pageSize);

        // 页码
        Integer pageId = ParamMapUtil.getInteger(jsonMap, "pageId");
        if (pageId == null) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "页码不能为空");
        }
        if (pageId <= 0) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "页码必须大于0");
        }
        platformBase.setPageId(pageId);

        // 查询列表
        PageInfo<PlatformBase> pageInfo = this.platformBaseService.queryByList(platformBase);

        List<PlatformBase> dataList = pageInfo.getData();
        Map<String,Object> pageMap = new HashMap<String,Object>();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        if (dataList != null && dataList.size() > 0) {
            
            // 整理返回数据
            for (PlatformBase tempData : dataList) {
                Map<String, Object> tempMap = new HashMap<String, Object>();
                tempMap.put("id", tempData.getId());
                tempMap.put("name", tempData.getName());
                tempMap.put("type", tempData.getType());
                tempMap.put("description", tempData.getDescription());
                tempMap.put("ownerId", tempData.getOwnerId());
                tempMap.put("cstCreate", tempData.getCstCreate());
                mapList.add(tempMap);
            }
        }
        pageMap.put("dataList", mapList);
        pageMap.put("rowCount", pageInfo.getPager().getRowCount());
        pageMap.put("pageCount", pageInfo.getPager().getPageCount());
        resultMap.put("pageMap", pageMap);
        return CommonResult.success("分页查询平台数据成功", resultMap);
    }

    /**
     * 
     * @Title: update
     * @Description: 修改平台数据进入页面
     * @author ZhangJianyang
     * @date 2025年06月16日 下午13:05:35
     * @param request
     * @param response
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @RequestMapping("/update")
    public CommonResult<Map<String, Object>> update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 参数数据
        Map<String, Object> jsonMap = getJsonMap(request);
        
        // 返回结果
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        // 主键ID
        Integer id = ParamMapUtil.getInteger(jsonMap, "id");
        if (id == null) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "该平台ID不可为空");
        }

        // 校验是否存在
        PlatformBase checkPlatformBase = platformBaseService.queryById(id);
        if (checkPlatformBase == null) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "该平台不存在");
        }
        Map<String, Object> tempMap = new HashMap<String, Object>();
        tempMap.put("id", checkPlatformBase.getId());
        tempMap.put("name", checkPlatformBase.getName());
        tempMap.put("type", checkPlatformBase.getType());
        tempMap.put("description", checkPlatformBase.getDescription());
        tempMap.put("ownerId", checkPlatformBase.getOwnerId());
        tempMap.put("cstCreate", checkPlatformBase.getCstCreate());
        resultMap.put("dataMap", tempMap);
        return CommonResult.success("获取平台数据成功", resultMap);
    }

    /**
     * 
     * @Title: addSave
     * @Description: 添加保存平台数据
     * @author ZhangJianyang
     * @date 2025年06月16日 下午13:05:35
     * @param request
     * @param response
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @Duplicate(validate = true)
    @RequestMapping("/addSave")
    public CommonResult<Map<String, Object>> addSave(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 参数数据
        Map<String, Object> jsonMap = getJsonMap(request);
        
        // 数据对象
        PlatformBase platformBase = new PlatformBase();
        
        // 平台名称
        String name = ParamMapUtil.getString(jsonMap, "name");
        if (StringUtils.isNotBlank(name)) {
            platformBase.setName(name);
        }
        
        // 平台类型
        String type = ParamMapUtil.getString(jsonMap, "type");
        if (StringUtils.isNotBlank(type)) {
            platformBase.setType(type);
        }
        
        // 平台描述
        String description = ParamMapUtil.getString(jsonMap, "description");
        if (StringUtils.isNotBlank(description)) {
            platformBase.setDescription(description);
        }
        
        // 负责人ID
        Integer ownerId = ParamMapUtil.getInteger(jsonMap, "ownerId");
        if (ownerId != null) {
            platformBase.setOwnerId(ownerId);
        }

        try {
            OperatorUtil.addOper(platformBase);
            platformBaseService.add(platformBase);
        } catch (Exception e) {
            // 打印错误日志
            logger.error("添加平台失败", e);
            return CommonResult.failed(ResultCode.BUSINESS_ERROR, "添加平台失败");
        }

        return CommonResult.success("添加平台成功");
    }

    /**
     * 
     * @Title: updateSave
     * @Description: 修改保存平台数据
     * @author ZhangJianyang
     * @date 2025年06月16日 下午13:05:35
     * @param request
     * @param response
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @Duplicate(validate = true)
    @RequestMapping("/updateSave")
    public CommonResult<Map<String, Object>> updateSave(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> jsonMap = getJsonMap(request);
        
        // 主键ID
        Integer id = ParamMapUtil.getInteger(jsonMap, "id");
        if (id == null) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "该平台ID不可为空");
        }

        // 是否存在校验
        PlatformBase checkPlatformBase = platformBaseService.queryById(id);
        if (checkPlatformBase == null) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "该平台不存在");
        }

        
        // 平台名称
        String name = ParamMapUtil.getString(jsonMap, "name");
        if (StringUtils.isNotBlank(name)) {
            checkPlatformBase.setName(name);
        }
        
        // 平台类型
        String type = ParamMapUtil.getString(jsonMap, "type");
        if (StringUtils.isNotBlank(type)) {
            checkPlatformBase.setType(type);
        }
        
        // 平台描述
        String description = ParamMapUtil.getString(jsonMap, "description");
        if (StringUtils.isNotBlank(description)) {
            checkPlatformBase.setDescription(description);
        }
        
        // 负责人ID
        Integer ownerId = ParamMapUtil.getInteger(jsonMap, "ownerId");
        if (ownerId != null) {
            checkPlatformBase.setOwnerId(ownerId);
        }

        try {
            OperatorUtil.updateOper(checkPlatformBase);
            platformBaseService.updateIncludeAll(checkPlatformBase);
        } catch (Exception e) {
            // 打印错误日志
            logger.error("修改平台失败", e);
            return CommonResult.failed(ResultCode.BUSINESS_ERROR, "修改平台失败");
        }

        return CommonResult.success("修改平台成功");
    }

    /**
     * 
     * @Title: delete
     * @Description: 删除平台数据
     * @author ZhangJianyang
     * @date 2025年06月16日 下午13:05:35
     * @param request
     * @param response
     * @throws Exception
     */
    @Auth(verifyLogin = true)
    @RequestMapping("/delete")
    public CommonResult<Map<String, Object>> delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> jsonMap = getJsonMap(request);
        Integer id = ParamMapUtil.getInteger(jsonMap, "id");
        if (id == null) {
            return CommonResult.failed(ResultCode.PARAM_EMPTY, "该平台ID不可为空");
        }

        // 是否存在校验
        PlatformBase checkPlatformBase = platformBaseService.queryById(id);
        if (checkPlatformBase == null) {
            return CommonResult.failed(ResultCode.PARAM_INVALID, "该平台不存在");
        }

        try {
            platformBaseService.deleteById(checkPlatformBase.getId());
        } catch (Exception e) {
            // 打印错误日志
            logger.error("删除平台失败", e);
            return CommonResult.failed(ResultCode.BUSINESS_ERROR, "删除平台失败");
        }

        return CommonResult.success("删除平台成功");
    }
}
